package com.zrar.ak.gkpt.service.impl.common;

import com.alibaba.druid.util.StringUtils;
import com.zrar.ak.gkpt.dao.IFormBuildUnitTDao;
import com.zrar.ak.gkpt.dao.IFormCirculationDetailsTDao;
import com.zrar.ak.gkpt.model.bo.FormBuildUnitTBO;
import com.zrar.ak.gkpt.model.bo.FormCirculationDetailsTBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.common.ICirculationDetailsService;
import com.zrar.ak.gkpt.util.BusinessEnum;
import com.zrar.ak.gkpt.util.CommonTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CirculationDetailsService implements ICirculationDetailsService {
    @Autowired
    private IFormCirculationDetailsTDao formCirculationDetailsTDao;
    @Autowired
    private IFormBuildUnitTDao formBuildUnitTDao;

    @Override
    @Transactional
    public Boolean saveCirculationDetails(String businessId, String businessType, String circulationStatus, String businessBuildUnitCode, String handlingOpinions, UserVO userVO){
        FormCirculationDetailsTBO circulationDetails = new FormCirculationDetailsTBO();
        //旧的流转明细处理(设置接收人编码，接收人姓名，接收时间，流转明细置为无效，并将旧的流转明细的id取出来放入新的流转明细中)
        FormCirculationDetailsTBO lastCirculationDetails = formCirculationDetailsTDao.getEffectiveCirculationDetailsByBusinessId(businessId);
        if(lastCirculationDetails!=null){
            lastCirculationDetails.setReceiveTime(new Date());
            lastCirculationDetails.setReceiverCode(userVO.getUserId());
            lastCirculationDetails.setReceiverName(userVO.getUserName());
            lastCirculationDetails.setIsEffective(BusinessEnum.IS_EFFECTIVE_N.getValue());
            formCirculationDetailsTDao.updateByPrimaryKey(lastCirculationDetails);
            circulationDetails.setLastCirculationId(lastCirculationDetails.getCirculationId());
        }else{
            lastCirculationDetails = new FormCirculationDetailsTBO();
        }

        //新的流转明细处理
        circulationDetails.setCirculationId(CommonTools.getUUID());
        circulationDetails.setBusinessType(businessType);
        circulationDetails.setBusinessId(businessId);
        circulationDetails.setCirculationStatus(circulationStatus);
        circulationDetails.setLastCirculationId(lastCirculationDetails.getCirculationId());
        circulationDetails.setBusinessBuildUnitCode(businessBuildUnitCode);

        circulationDetails.setHandlingOpinions(handlingOpinions);
        circulationDetails.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        circulationDetails.setIsEffective(BusinessEnum.IS_EFFECTIVE_Y.getValue());

        //发起人相关信息
        circulationDetails.setOriginateTime(new Date());
        circulationDetails.setOriginatorCode(userVO.getUserId());
        circulationDetails.setOriginatorName(userVO.getUserName());
        circulationDetails.setOriginatorUnitCode(userVO.getUnitCode());
        circulationDetails.setOriginatorUnitName(userVO.getUnitName());

        //接收人相关信息
        circulationDetails.setReceiverCode(null);
        circulationDetails.setReceiverName(null);
        String receiverUnitCode = null;
        String receiverUnitName = null;
        FormBuildUnitTBO formBuildUnitTBO = new FormBuildUnitTBO();
        if(BusinessEnum.CIRCULATION_STATUS_ASSIGN.getValue().equals(circulationStatus) || BusinessEnum.CIRCULATION_STATUS_RETURN.equals(circulationStatus)){
            //下发和退回时，接收单位为建设单位
            formBuildUnitTBO.setBuildUnitCode(businessBuildUnitCode);
            formBuildUnitTBO = formBuildUnitTDao.selectOne(formBuildUnitTBO);
            receiverUnitCode = businessBuildUnitCode;
            receiverUnitName = formBuildUnitTBO.getBuildUnitName();
        }else if(BusinessEnum.CIRCULATION_STATUS_REPLY.equals(circulationStatus)){
            //回复时，接收单位为上级单位
            formBuildUnitTBO.setBuildUnitType("1");
            formBuildUnitTBO = formBuildUnitTDao.selectOne(formBuildUnitTBO);
            receiverUnitCode = formBuildUnitTBO.getBuildUnitCode();
            receiverUnitName = formBuildUnitTBO.getBuildUnitName();
        }else if(BusinessEnum.CIRCULATION_STATUS_END.equals(circulationStatus)){
            //结束时，接收单位接收人均为自己，接收时间为当前时间,且流转明细直接置为无效
            receiverUnitCode = userVO.getUnitCode();
            receiverUnitName = userVO.getUnitName();
            circulationDetails.setReceiverCode(userVO.getUserId());
            circulationDetails.setReceiverName(userVO.getUserName());
            circulationDetails.setReceiveTime(new Date());
            circulationDetails.setIsEffective(BusinessEnum.IS_EFFECTIVE_N.getValue());
        }
        circulationDetails.setReceiverUnitCode(receiverUnitCode);
        circulationDetails.setReceiverUnitName(receiverUnitName);
        int insert = formCirculationDetailsTDao.insert(circulationDetails);
        return true;
    }

    @Override
    public Boolean canRecordAssign(String businessType, String buildUnitCode) {
        Map map = new HashMap<String,String>();
        map.put("businessType",businessType);
        map.put("buildUnitCode",buildUnitCode);
        Boolean bool = formCirculationDetailsTDao.canRecordAssign(map);
        return bool;
    }

    @Override
    public Map getCirculeInfo(String businessId, String businessStatus) {
        Map map = new HashMap<String,String>();
        Map circuleInfo = new HashMap();
        map.put("businessId",businessId);
        if(BusinessEnum.BUSINESS_STATUS_REPLY_N.getValue().equals(businessStatus)){
            //下发------未回复
            map.put("circulationStatus",BusinessEnum.CIRCULATION_STATUS_ASSIGN.getValue());
            //无处理意见
            //处理意见不展示(实际上无)
            circuleInfo.put("handlingOpinionsShow","0");
        }else if(BusinessEnum.BUSINESS_STATUS_REPLY_Y.equals(businessStatus)){
            //回复------已回复(可能需要展示历史处理意见)
            map.put("circulationStatus",BusinessEnum.CIRCULATION_STATUS_REPLY.getValue());
            //处理意见不展示
            circuleInfo.put("handlingOpinionsShow","0");
        }else if(BusinessEnum.BUSINESS_STATUS_RETURN.equals(businessStatus)){
            //退回修改------退回修改
            map.put("circulationStatus",BusinessEnum.CIRCULATION_STATUS_RETURN.getValue());
            //需要携带对应流转明细的处理意见(一个处理意见)
            FormCirculationDetailsTBO circulationDetails = formCirculationDetailsTDao.getCurrentCirculeHandlingOpinions(map);
            if(circulationDetails==null || StringUtils.isEmpty(circulationDetails.getHandlingOpinions())){
                circuleInfo.put("handlingOpinionsShow","0");
            }else{
                circuleInfo.put("handlingOpinionsShow","1");
                circuleInfo.put("handlingOpinions",circulationDetails.getHandlingOpinions());
            }
        }else if(BusinessEnum.BUSINESS_STATUS_RECORD_Y.equals(businessStatus)) {
            //结束------已备案
            map.put("circulationStatus",BusinessEnum.CIRCULATION_STATUS_END.getValue());
            //无处理意见
            circuleInfo.put("handlingOpinionsShow","0");
        }else{
            circuleInfo.put("handlingOpinionsShow","0");
        }
        return circuleInfo;
    }


}
